package com.nd.android.u.ui.chatUiUtils;

import ims.IMSdk;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.content.Context;
import android.text.format.DateFormat;

import com.nd.android.u.ui.R;
import com.product.android.business.ApplicationVariable;

public class ChatTimeUtils {
	private static final SimpleDateFormat todayRecentSdf = new SimpleDateFormat("今天HH:mm");
	private static final SimpleDateFormat yesterdayRecentSdf = new SimpleDateFormat("昨天HH:mm");
	private static final SimpleDateFormat beforeYesterDayRecentSdf = new SimpleDateFormat("前天HH:mm");
	private static final SimpleDateFormat dateChatSdf = new SimpleDateFormat("今天HH:mm");
	private static final SimpleDateFormat yesterdayChatSdf = new SimpleDateFormat("昨天HH:mm");
	private static final SimpleDateFormat beforeYesterDayChatSdf = new SimpleDateFormat("前天HH:mm");
	private static final SimpleDateFormat otherRecentSdf = new SimpleDateFormat("MM-dd HH:mm");
	private static final SimpleDateFormat otherChatSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final SimpleDateFormat monthformat = new SimpleDateFormat( "MM-dd" );
	
	private static final SimpleDateFormat otheryearRecentSdf = new SimpleDateFormat("yyyy年MM月dd日");
	private static final SimpleDateFormat monthHourRecentformat = new SimpleDateFormat( "MM月dd号HH:mm" );
	private static final SimpleDateFormat monthRecentformat = new SimpleDateFormat( "MM月dd号" );
	private static final SimpleDateFormat yesterdayearRecentSdf = new SimpleDateFormat("昨天凌晨HH:mm");
	private static final SimpleDateFormat yesterdaymorRecentSdf = new SimpleDateFormat("昨天早上HH:mm");
	private static final SimpleDateFormat yesterdayaftRecentSdf = new SimpleDateFormat("昨天下午HH:mm");
	private static final SimpleDateFormat yesterdaynigRecentSdf = new SimpleDateFormat("昨天晚上HH:mm");
	public static final int TYPE_FUTURE = 0; // 未来
	public static final int TYPE_BEFORE_YEAR = 1; // 年前
	public static final int TYPE_BEFORE_MONTH = 2; // 月前
	public static final int TYPE_BEFORE_DAY = 3; // 日前
	public static final int TYPE_TODAY = 4; // 今日
	public static Date getNowTime(){
		Calendar calendar = Calendar.getInstance();
		Date nowDate = calendar.getTime();		
		return nowDate;
	}
	
	public static  SimpleDateFormat getChatSdf (){
		return otherChatSdf;
	}
	
	public static String getMsgTime(Date date){
		if(date ==null) return "";
		else return otherRecentSdf.format(date);
	}
	
	public static String getNowTimeStr(){
		return getMsgTime(getNowTime());
	}
	
	public static String getMsgHourFormatStr(Date date,int type){
		if(type==0)
			return todayRecentSdf.format(date);
		else return dateChatSdf.format(date);
	}
	
	public static String getMsgYesterdayFormatStr(Date date,int type){
		if(type==0)
			return yesterdayRecentSdf.format(date);
		else return yesterdayChatSdf.format(date);
	}
	
	public static String getMsgBeforeYesterdayFormatStr(Date date,int type){
		if(type==0)
			return beforeYesterDayRecentSdf.format(date);
		else return beforeYesterDayChatSdf.format(date);
	}
	
	public static String getMsgMonthHourFormatStr(Date date){
		return monthformat.format(date);
	}
	
	public static int isTodayOrYesterDay( Date a ){
		Calendar c = Calendar.getInstance();
		Date today = c.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd" );
		if(format.format( today ).equals(format.format( a ) )) return 1;
		return 0;
	}
	
	 public static String parseDate(Date createTime,int type){    
		SimpleDateFormat datesdf1 = null;
		SimpleDateFormat datesdf2 = null;
		SimpleDateFormat datesdf3 = null;
		SimpleDateFormat datesdf4 = null;
		if(type==0){
			datesdf1= todayRecentSdf;
			datesdf2= yesterdayRecentSdf;
			datesdf3= beforeYesterDayRecentSdf;
			datesdf4= otherRecentSdf;
		 }
		 else{
			datesdf1= dateChatSdf;
			datesdf2= yesterdayChatSdf;
			datesdf3= beforeYesterDayChatSdf;
			datesdf4= otherChatSdf;
		 }
	     try {
	         String ret = "";
	         Calendar calendar = Calendar.getInstance();
	         calendar.setTime(createTime);
	         long create =calendar.getTimeInMillis();
	         Calendar now = Calendar.getInstance();
	         long ms  = 1000*(now.get(Calendar.HOUR_OF_DAY)*3600+now.get(Calendar.MINUTE)*60+now.get(Calendar.SECOND));//毫秒数
	         long ms_now = now.getTimeInMillis();
	         if(ms_now-create<ms){
	             ret = datesdf1.format(createTime);
	         }else if(ms_now-create<(ms+24*3600*1000)){
	             ret = datesdf2.format(createTime);
	         }else if(ms_now-create<(ms+24*3600*1000*2)){
	             ret = datesdf3.format(createTime);
	         }else{
	             ret= datesdf4.format(createTime);
	         }
	         	return ret;
	         } catch (Exception e) {
	          	e.printStackTrace();
	          	return "";
	     }
	}
	 
	 public static boolean comparent(Date date1,Date date2 ){
		 if(date1==null||date2==null) return true;
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date1);
		 long time1 = calendar.getTimeInMillis();
		 calendar.setTime(date2);
		 long time2 = calendar.getTimeInMillis();
		 if((time2-time1)>3*60*1000)
			return true;
		 else return false;
	}
	 
	 public static String getLastTime(Date date){
		 String ret = "";
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
//		 Log.v(TAG,"service'time1:"+cal.getTimeInMillis());
//		 Log.v(TAG,"service'time2:"+System.currentTimeMillis());
		 
		 long diff = (System.currentTimeMillis()-cal.getTimeInMillis())/1000;
//		 Log.v(TAG,"service'time3:"+diff);
		 if(diff>=0&&diff<60){
			 ret= 1+ApplicationVariable.INSTANCE.applicationContext.getString(R.string.chat_min_ago);
		 }
		 else if(diff>=60&&diff<60*60){
			 ret= (diff/60+(((diff/60)%60)==0?0:1))+ApplicationVariable.INSTANCE.applicationContext.getString(R.string.chat_min_ago);
		 }else if(diff>=60*60&&diff<24*60*60){
			 ret=(diff/3600+(((diff/3600)%3600)==0?0:1))+ApplicationVariable.INSTANCE.applicationContext.getString(R.string.chat_hour_ago);
		 }else if(diff>=24*60*60&&diff<48*60*60){
			 int hour=cal.get(Calendar.HOUR_OF_DAY);
        	 if(hour>=0&&hour<6){
        		 ret = yesterdayearRecentSdf.format(date);
        	 }else if(hour>=6&&hour<12){
        		 ret = yesterdaymorRecentSdf.format(date);
        	 }else if(hour>=12&&hour<18){
        		 ret = yesterdayaftRecentSdf.format(date);
        	 }else{
        		 ret = yesterdaynigRecentSdf.format(date);
        	 }	             			
		 }else {                                       //>48小时或者<0
			 Calendar now = Calendar.getInstance();
			 if(cal.get(Calendar.YEAR)!=now.get(Calendar.YEAR)){   //跨年
        		 ret= otheryearRecentSdf.format(date);	        		 
        	 }else{
        		 if(diff<0){
        			 ret=monthHourRecentformat.format(date);	 
        		 }else{
        			 ret=monthRecentformat.format(date);	  
        		 }	  
        	 }			 
		 }
		 return  ret;
		 
	 }
	 
	public static String getLastTime(long timeMillis) {
//		Log.v(TAG,"service'time:"+timeMillis);
		Date date = new Date(timeMillis);
		return getLastTime(date);
	}
	 
	 
	    /**
	     * 把时间转换成yyyy-mm-dd 或者 mm-dd
	     * @param timestamp 单位ms
	     * @return
	     */
	    public static String formateMillisTime(long timestamp) {
	    	int currentYear = Calendar.getInstance().get(Calendar.YEAR);
	    	Calendar calendar = Calendar.getInstance();
	    	calendar.setTimeInMillis(timestamp);
	    	int year = calendar.get(Calendar.YEAR);
	    	
	    	if (currentYear > year) { // yyyy-mm-dd
	    		return DateFormat.format("yyyy-MM-dd", timestamp).toString(); 
	    	} else { // mm-dd
	    		return DateFormat.format("MM-dd", timestamp).toString(); 
	    	}
	    }
	    
	    /**
	     * 把时间转换成yyyy-mm-dd 或者 mm-dd
	     * @param timestamp 单位s
	     * @return
	     */
	    public static String formateEpochTime(long timestamp) {
	    	return formateMillisTime(timestamp * 1000);
	    }
	
	    static public String GetDateString(long anchor) {

	        StringBuffer sb = new StringBuffer("");
	        long now = System.currentTimeMillis();
	        long aday = 24 * 60 * 60 * 1000;
	        long yesterday = now - aday;

	        String todayDate = GetDate(now);
	        String yesterdayDate = GetDate(yesterday);
	        String anchorDate = GetDate(anchor);

	        if (anchorDate.equals(todayDate)) {
	            sb.append("今天");
	        } else if (anchorDate.equals(yesterdayDate)) {
	            sb.append("昨天");
	        } else {
	            sb.append(anchorDate);
	        }

	        sb.append(" ");

	        String time = GetTime(anchor);
	        sb.append(time);

	        return sb.toString();
	    }

	    static public String GetDate(long date) {
	        CharSequence val = DateFormat.format("yyyy-MM-dd", new Date(date));
	        return val.toString();
	    }

	    static public String GetTime(long date) {
	        CharSequence val = DateFormat.format("k:mm", new Date(date));
	        return val.toString();
	    }

	    public static String GetFullTime(long date) {
	        CharSequence val = DateFormat.format("yyyy-MM-dd k:mm:ss", new Date(date));
	        return val.toString();
	    }

	    static public String GetDurationString(int nDuration) {
	        int nHour = 0;
	        int nMin = 0;
	        int nSec = 0;

	        nHour = nDuration / 3600;
	        nMin = nDuration / 60;
	        nSec = nDuration % 60;

	        StringBuffer strTime = new StringBuffer("");

	        if (nHour != 0) {
	            strTime.append(nHour);
	            strTime.append(" 小时 ");
	        }

	        strTime.append(nMin);
	        strTime.append(" 分钟 ");

	        strTime.append(nSec);
	        strTime.append(" 秒 ");

	        return strTime.toString();
	    }

	    /*
	     * 将YYYY-MM-DD格式的日期转换成Unix时间戳
	     */
	    static public long getTimeStamp(String date) {
	        if (null == date || date.length() < 1)
	            return 0;
	        String arrDate[] = date.split("-");
	        if (arrDate.length == 3) {
	            return Date.UTC(Integer.valueOf(arrDate[0]) - 1900,
	                    Integer.valueOf(arrDate[1]) - 1,
	                    Integer.valueOf(arrDate[2]), 0, 0, 0);

	        } else if (arrDate.length == 2)
	            return Date.UTC(0,
	                    Integer.valueOf(arrDate[0]) - 1,
	                    Integer.valueOf(arrDate[1]), 0, 0, 0);
	        return 0;
	    }

	    public static String getTime(Context context, long timeStamp) {
	    	return getTime(timeStamp);
	    }
	    
	    public static String getTime(long timeStamp) {
	        long tempTime = timeStamp * (timeStamp < 10000000000L ? 1000 : 1);
	        long now = System.currentTimeMillis();

	        long aday = 24 * 60 * 60 * 1000;
	        long yesterday = now - aday;
	        long beforeYesterday = now - aday * 2;
	        long week = now - aday * 6;

	        String todayDate = GetDate(now);
	        String yesterdayDate = GetDate(yesterday);
	        String beforeYesterdayDate = GetDate(beforeYesterday);
	        String weekDate = GetDate(week);
	        long weekTime = getTimeStamp(weekDate);
	        String anchorDate = GetDate(tempTime);
	        String format = "MM-dd HH:mm";
	        String prefix = "";
	        if (anchorDate.equals(todayDate)) {
	            format = "HH:mm";
	        } else if (now - tempTime >= 0) {
	            if (anchorDate.equals(yesterdayDate)) {
	                format = "HH:mm";
	                prefix = "昨天";
	            } else if (anchorDate.equals(beforeYesterdayDate)) {
	                format = "HH:mm";
	                prefix = "前天";
	            } else if (tempTime > weekTime) {
	                format = "E HH:mm";
	            }
	        }
	        return prefix + getTimeFormat(
	                format,
	                tempTime
	                        + TimeZone.getTimeZone("GMT+8:00 ").getRawOffset()
	                        - TimeZone.getDefault().getRawOffset());
	    }

	    public static String getTimeFormat(String format, long time) {
	        SimpleDateFormat formatter = new SimpleDateFormat(format);
	        Date curDate = new Date(time);// 获取当前时间
	        return formatter.format(curDate);
	    }
	    
	    public static String getCurrentTime() {
	        long now = System.currentTimeMillis();
	        String todayDate = GetDate(now);
	        String nowTime = getTime(now);
	        return todayDate + " " +  nowTime;
	    }
	    
	  /**
	 * @n<b>函数名称</b>     :getTimeType
	 * @brief 获取时间的类型（和当前时间比较）
	 * @version    Ver 1.0.0
	 * @param  @param time
	 * @param  @return
	 * @return int
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-4-16下午3:50:44      
	*/
	public static int getTimeType(long time){
		  long now=IMSdk.computeServertime()*1000L;
		  if (time>now){
			  //时间大于现在的时间
			  return TYPE_FUTURE;
		  }
		  Calendar target=Calendar.getInstance();
		  target.setTimeInMillis(time);
		  Calendar nowDate=Calendar.getInstance();
		  nowDate.setTimeInMillis(now);
		  int targetYear=target.get(Calendar.YEAR);
		  int nowYear=nowDate.get(Calendar.YEAR);
		  if (targetYear<nowYear){
			  //年前
			  return TYPE_BEFORE_YEAR;
		  }
		  int targetMonth=target.get(Calendar.MONTH);
		  int nowMonth=target.get(Calendar.MONTH);
		  if (targetMonth<nowMonth){
			  //月前
			  return TYPE_BEFORE_MONTH;
		  }
		  if (target.get(Calendar.DAY_OF_MONTH) <nowDate.get(Calendar.DAY_OF_MONTH)){
			  //非本日
			  return TYPE_BEFORE_DAY;
		  }
		  return TYPE_TODAY;
	  }
}